package com.tmz.utils;

import java.io.PrintWriter;
import java.io.StringWriter;

import org.apache.log4j.Logger;

import com.opensymphony.xwork2.util.location.Location;

public class QuinielaException extends RuntimeException{
	static Logger logger = Logger.getLogger(QuinielaException.class);
	
	private String messTitle;        
	private String messClearForUser;
	private String messStack;
	private String messLog;
	

	/**
	 * 
	 */
	public QuinielaException() {
		super();
	}

	/**
	 * @param message
	 */
	public QuinielaException(String message,boolean log) {
		super(message);
		if (log)
		{
			messLog = message;
			this.writeLog();
		}

	}
	/**
	 * Por defecto se escribe el mensaje en el log
	 * @param message
	 */
	public QuinielaException(String message) {
		super(message);
		messLog = message;
		this.writeLog();

	}	

	/**
	 * @param cause
	 */
	public QuinielaException(Throwable cause,boolean log) {
		super(cause);
		if (log)
		{
			messLog = this.getTraceException(cause);
			this.writeLog();
		}
	}
	/**
	 * @param cause
	 */
	public QuinielaException(Throwable cause) {
		super(cause);
		messLog = this.getTraceException(cause);
		this.writeLog();
	}	

	/**
	 * @param message
	 * @param cause
	 */
	public QuinielaException(String message, Throwable cause,boolean log) {
		super(message, cause);
		if (log)
		{
			messLog = this.getTraceException(cause);
			this.writeLog();
		}		
	}

	/**
	 * @param message
	 * @param cause
	 */
	public QuinielaException(String message, Throwable cause) {
		super(message, cause);
		messLog = this.getTraceException(cause);
		this.writeLog();
	}	


	/**
	 * @return
	 */
	public String getMessClearForUser() {
		return messClearForUser;
	}

	/**
	 * @return
	 */
	public String getMessStack() {
		return messStack;
	}

	/**
	 * @return
	 */
	public String getMessTitle() {
		return messTitle;
	}

	/**
	 * @param string
	 */
	public void setMessClearForUser(String string) {
		messClearForUser = string;
	}

	/**
	 * @param string
	 */
	public void setMessStack(String string) {
		messStack = string;
	}

	/**
	 * @param string
	 */
	public void setMessTitle(String string) {
		messTitle = string;
	}
	
	/*
		* Devuelve la traza de cualquier Throwable (Error o Exception)
		* 
		* @param exception Throwable de la que se quiere obtener su traza
		* 
		* @return Traza de un Throwable
		*/
	 private String getTraceException(Throwable exception)
	 {
	   String result = null;
	   try
	   {
		 StringWriter sw = new StringWriter();
		 PrintWriter ps = new PrintWriter(sw);
		 exception.printStackTrace(ps);
		 result = sw.toString();
	   }
	   catch (Throwable thr)
	   {
		 result = "";
	   }
	   return result;
	 }

	 private void writeLog()
	 {
		logger.error(messLog);
	 }
}
